package com.atguigu.lease.web.admin.controller.system;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


@Tag(name = "后台用户信息管理！(SystemUserController)")
@RestController
@RequestMapping("/admin/system/user")
public class SystemUserController {

    @Autowired
    private SystemUserService service;

    @Operation(summary = "根据条件分页查询后台用户列表")
    @GetMapping("page")
    public Result<IPage<SystemUserItemVo>> page(@RequestParam long current, @RequestParam long size, SystemUserQueryVo queryVo) {
        Page<SystemUserItemVo> systemUserItemVoPage = service.pageSystemUserItemVOList(new Page<SystemUserItemVo>(current, size), queryVo);
        return Result.ok(systemUserItemVoPage);
    }

    @Operation(summary = "根据ID查询后台用户信息")
    @GetMapping("getById")
    public Result<SystemUserItemVo> getById(@RequestParam Long id) {
        SystemUserItemVo systemUserItemVo = service.getSystemUserItemVOById(id);
        return Result.ok(systemUserItemVo);
    }

    @Operation(summary = "保存或更新后台用户信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SystemUser systemUser) {
        //判断新增还是修改null为新增
        if (systemUser.getId() == null) {
            //加密密码
            systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
            systemUser.setStatus(BaseStatus.ENABLE);
            //默认头像
            systemUser.setAvatarUrl("http://192.168.178.144:9000/lease/IMG_0946.JPG");
        } else {
            if (!StringUtils.isEmpty(systemUser.getPassword())) {
                //修改只有密码需要经过处理 加密一下再放回到对象中
                systemUser.setPassword(DigestUtils.md5Hex(systemUser.getPassword()));
            }
        }
        //调用service，做更新保存操作
        service.saveOrUpdate(systemUser);
        return Result.ok();
    }

    @Operation(summary = "判断后台用户名是否可用")
    @GetMapping("isUserNameAvailable")
    public Result<Boolean> isUsernameExists(@RequestParam String username) {
        SystemUser one = service.getOne(new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getUsername, username));
        return Result.ok(one == null);
    }

    @DeleteMapping("deleteById")
    @Operation(summary = "根据ID删除后台用户信息")
    public Result removeById(@RequestParam Long id) {
        service.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据ID修改后台用户状态")
    @PostMapping("updateStatusByUserId")
    public Result updateStatusByUserId(@RequestParam Long id, @RequestParam BaseStatus status) {
        service.update(new LambdaUpdateWrapper<SystemUser>().set(SystemUser::getStatus, status).eq(SystemUser::getId, id));
        return Result.ok();
    }
}
